using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class ManageUsers : Form
    {
        Insert i;
        DataTable accounts;
        public ManageUsers()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (textBox2.Visible)
            {
                textBox2.Visible = false;
                button4.Text = "Change";
                textBox2.Text = "";
            }
            else
            {
                textBox2.Visible = true;
                button4.Text = "Undo";
                textBox2.Text = "";
            }
        }

        private void ManageUsers_Load(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 2)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            i = new Insert(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            retrieve();
            
            
        }
        public void retrieve()
        {
            retrieve r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            accounts =r.getaccounts();
            dataGridView1.DataSource = accounts;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[3].Visible = false;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(accounts.Rows[dataGridView1.SelectedCells[0].RowIndex][0]);

            
            if (i.deleteaccount(id, ((Container)this.MdiParent).id))
                MessageBox.Show("User Deleted SuccessFully"); 
            else
                MessageBox.Show("Error Occured "); ;

            this.retrieve();

            
            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AddUser ad = new AddUser();
            ad.parent = this;
            ad.MdiParent = this.MdiParent;
            ad.Show();

        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.SelectedCells.Count < 1)
                return;
            textBox1.Text = accounts.Rows[dataGridView1.SelectedCells[0].RowIndex][1].ToString();
            //textBox2.Text = accounts.Rows[dataGridView1.SelectedCells[0].RowIndex][3].ToString();
            int sec = Convert.ToInt32(accounts.Rows[dataGridView1.SelectedCells[0].RowIndex][2]);

            if (sec == 1)
                radioButton3.Checked = true;
            else if (sec == 2)
                radioButton2.Checked = true;
            else if (sec == 3)
                radioButton3.Checked = true;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int sec = -1;
            int id = Convert.ToInt32(accounts.Rows[dataGridView1.SelectedCells[0].RowIndex][0]);
            if(radioButton1.Checked)
                sec = 3;
            else if (radioButton2.Checked)
                sec = 2;
            else if (radioButton3.Checked)
                sec = 1;

            if(i.updateaccount(textBox1.Text, (textBox2.Visible ? textBox2.Text : ""), sec, id, ((Container)this.MdiParent).id))
                MessageBox.Show("Update Succesfully");
            else
                MessageBox.Show("Update Failed");
            this.retrieve();
            this.MdiParent.Refresh();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            
        }

    }
}